package com.fjrz.bbxwj.mode.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.fjrz.bbxwj.entity.*
import com.fjrz.bbxwj.mode.api.*
import com.module.lemlin.base.BaseViewModel
import com.module.lemlin.mode.ViewModelSubscribe
import com.module.lemlin.rxhttp.HttpServiceData
import com.module.lemlin.rxhttp.HttpServiceResponse
import io.reactivex.rxjava3.disposables.Disposable


class MessageViewModel(application: Application) : BaseViewModel(application) {


    var page: Int = 1
    var limit: Int = 10

    val messageInfoLiveData = MutableLiveData<HttpServiceResponse<MessageInfoResponse>>()

    /**
     * 消息中心（最新公告|最新系统消息|未读公告数|未读系统消息数）
     * */
    fun messageInfo() {
        val disposable: Disposable =
            messageInfo(ViewModelSubscribe(messageInfoLiveData))
        dispose.add(disposable)
    }

    val messageAnnouncementsLiveData = MutableLiveData<HttpServiceResponse<AnnouncementResponse>>()

    /**
     * 公告列表
     * */
    fun messageAnnouncements(isLoadMore: Boolean = false) {
        val listener =
            object : ViewModelSubscribe<AnnouncementResponse>(messageAnnouncementsLiveData) {
                override fun onSuccess(resp: AnnouncementResponse) {
                    resp.loadMore = page > 1
                    super.onSuccess(resp)
                    page += 1
                }
            }
        if (!isLoadMore)
            page = 1
        val disposable: Disposable =
            messageAnnouncements(page.toString(), limit.toString(), listener)
        dispose.add(disposable)
    }


    val messageAnnouncementsDetailsLiveData =
        MutableLiveData<HttpServiceResponse<AnnouncementDetailsResponse>>()

    /**
     * 公告详情
     *
     * @param announcementId
     * */
    fun messageAnnouncementsDetails(announcementId: Int) {
        val disposable: Disposable =
            messageAnnouncementsDetails(
                announcementId.toString(),
                ViewModelSubscribe(messageAnnouncementsDetailsLiveData)
            )
        dispose.add(disposable)
    }


    val messageSystemLiveData = MutableLiveData<HttpServiceResponse<MessageSystemResponse>>()

    /**
     * 系统消息列表
     * */
    fun messageSystem(isLoadMore: Boolean = false) {
        val listener = object : ViewModelSubscribe<MessageSystemResponse>(messageSystemLiveData) {
            override fun onSuccess(resp: MessageSystemResponse) {
                resp.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        val disposable: Disposable =
            messageSystem(page.toString(), limit.toString(), listener)
        dispose.add(disposable)
    }


    val messageSystemDetailsLiveData =
        MutableLiveData<HttpServiceResponse<MessageSystemDetailsResponse>>()

    /**
     * 系统消息详情
     *
     * @param messageId
     * */
    fun messageSystemDetails(messageId: Int) {
        val disposable: Disposable =
            messageSystemDetails(
                messageId.toString(), ViewModelSubscribe(messageSystemDetailsLiveData)
            )
        dispose.add(disposable)
    }


    val messageLikeLiveData = MutableLiveData<HttpServiceResponse<MessageLikeResponse>>()

    /**
     * 互动消息 - 赞
     * */
    fun messageLike(isLoadMore: Boolean = false) {
        val listener = object : ViewModelSubscribe<MessageLikeResponse>(messageLikeLiveData) {
            override fun onSuccess(resp: MessageLikeResponse) {
                resp.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        val disposable: Disposable =
            messageLike(page.toString(), limit.toString(), listener)
        dispose.add(disposable)
    }


    val messageCommentLiveData = MutableLiveData<HttpServiceResponse<MessageCommentResponse>>()

    /**
     * 互动消息 - 评论
     * */
    fun messageComment(isLoadMore: Boolean = false) {
        val listener = object : ViewModelSubscribe<MessageCommentResponse>(messageCommentLiveData) {
            override fun onSuccess(resp: MessageCommentResponse) {
                resp.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        val disposable: Disposable =
            messageComment(page.toString(), limit.toString(), listener)
        dispose.add(disposable)
    }


    val messageReplyLiveData = MutableLiveData<HttpServiceResponse<MessageReplyResponse>>()

    /**
     * 互动消息 - 评论
     * */
    fun messageReply(isLoadMore: Boolean = false) {
        val listener = object : ViewModelSubscribe<MessageReplyResponse>(messageReplyLiveData) {
            override fun onSuccess(resp: MessageReplyResponse) {
                resp.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        val disposable: Disposable =
            messageReply(page.toString(), limit.toString(), listener)
        dispose.add(disposable)
    }

    val chatIndexLiveData = MutableLiveData<HttpServiceResponse<ChatIndexResponse>>()

    /**
     * 聊天列表
     * */
    fun messageChatIndex() {
        val disposable: Disposable =
            messageChatIndex(ViewModelSubscribe(chatIndexLiveData))
        dispose.add(disposable)
    }

    val chatListLiveData = MutableLiveData<HttpServiceResponse<ChatListResponse>>()
    /**
     * 聊天详情
     * */
    fun messageChatList(userId: Int) {
        val disposable: Disposable =
            messageChatList(userId.toString(), ViewModelSubscribe(chatListLiveData))
        dispose.add(disposable)
    }
}